//
//  SLAccessListCell.m
//  SlockApp
//
//  Created by 凯朱 on 2024/7/26.
//

#import "SLAccessListCell.h"

@implementation SLAccessListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)setModel:(SLLockKeyModel *)model{
    if ([kSTRING(model.isUsbKey) isEqualToString:@"1"]) {
        _titleLbl.text = kReplace(model.keyName,kLocalString(@"UsbKey member"));
    }else{
        _titleLbl.text = kReplace(model.keyName, @"-");
    }
    _typeLbl.textColor = KColor_Y;
    
    NSString *keyTypeName = @"";
    _vLine.hidden = YES;
    _descLbl.text = kSTRING(model.desc);
    if (model.keyType == 1) {
        keyTypeName = @"Permanent";
    }else if (model.keyType == 2){
        keyTypeName = @"Duration";
        if ([self isOverTime:model]) {
            _typeLbl.textColor = kHexColor(@"#808080");
            _vLine.hidden = NO;
        }
    }else if (model.keyType == 3){
        keyTypeName = @"One Time";
        if ([model.isUse isEqualToString:@"1"] || [self isOverTime:model]) {
            _typeLbl.textColor = kHexColor(@"#808080");
            _vLine.hidden = NO;
        }
    }
    _typeLbl.text = keyTypeName;
}

-(BOOL)isOverTime:(SLLockKeyModel *)model{
    NSDate *start = [NSDate dateWithString:model.startTime format:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *end = [NSDate dateWithString:model.endTime format:@"yyyy-MM-dd HH:mm:ss"];
    if (model.weekDays.count > 0) {
        NSString *str1 = [NSString dateStrFromDate:start formatStr:@"yyyy-MM-dd"];
        NSString *str2 = [NSString dateStrFromDate:end formatStr:@"yyyy-MM-dd"];
        str1 = [NSString stringWithFormat:@"%@ %@:00",str1,model.dayStart];
        str2 = [NSString stringWithFormat:@"%@ %@:00",str2,model.dayEnd];
        start = [NSDate dateWithString:str1 format:@"yyyy-MM-dd HH:mm:ss"];
        end = [NSDate dateWithString:str2 format:@"yyyy-MM-dd HH:mm:ss"];
    }
    //时间段钥匙还要判断是否在循环天内
    BOOL isOver = ![[NSDate date] isBetweenStartTime:start endTime:end];
    return isOver;
}


+(CGFloat)cellHeight:(SLLockKeyModel *)model{
    
    if (model.desc.length > 0) {
        return 66;
    }
    return 50;
}

@end
